package operation;

import Book.Book;
import Book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:26554
 * Date:2024-11-17
 * Time:13:15
 */
public class DeleteOperation implements IOPeration{
    public void work(Booklist booklist){
        int ret = -1;
        System.out.println("删除图书");
        System.out.println("请输入要删除的书的书名");
        Scanner san = new Scanner(System.in);
        String DeleteBookName = san.nextLine();
        int currentsize = booklist.getActualbooks();
        for (int i = 0; i < currentsize; i++) {
            String name = booklist.getbook(i).getName();
            if(DeleteBookName.equals(name)){
                ret = i;
                break;
            }
        }
        if(ret==-1){
            System.out.println("该书不存在");
        }else {
            for (int i = ret; i < currentsize-1; i++) {
              Book book = booklist.getbook(ret+1);
              booklist.setBook(ret,book);
            }
            booklist.setActualbooks(currentsize-1);
            System.out.println("删除成功");
        }
    }
}
